package com.example.famhealing.init;

import com.example.famhealing.R;
import com.example.famhealing.R.drawable;

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.PointF; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.VelocityTracker; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Scroller; 
import android.widget.Toast; 

public class SlidingView extends ViewGroup {
    private static final String TAG = "SlidingView"; 
    private VelocityTracker mVelocityTracker = null; 
    private static final int SNAP_VELOCITY = 100; 
    private int mTouchSlop = 10; 
    private Bitmap mWallpaper = null;
    private Paint mPaint = null;
    private Scroller mScroller = null; 
    private PointF mLastPoint = null;
    private int mCurPage = 0;
    private int mCurTouchState;
    private static final int TOUCH_STATE_SCROLLING = 0;
    private static final int TOUCH_STATE_NORMAL = 1; 
    private Toast mToast; 

    public SlidingView(Context context) { 
        super(context); 
        init(); 
    } 

    public SlidingView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        init(); 
    } 

    public SlidingView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle); 
        init(); 
    } 

    private void init() { 
        mWallpaper = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        mPaint = new Paint(); 
        mScroller = new Scroller(getContext());
        mLastPoint = new PointF(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
        Log.d(TAG, "onMeasure"); 
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);  
        } 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.d(TAG, "onLayout"); 
        for (int i = 0; i < getChildCount(); i++) {
            int w = getChildAt(i).getMeasuredWidth() * i; 
            getChildAt(i).layout(w, t, w + getChildAt(i).getMeasuredWidth(), 
                    getChildAt(i).getMeasuredHeight()); 
        } 
    } 

    @Override 
    protected void dispatchDraw(Canvas canvas) {
        canvas.drawBitmap(mWallpaper, 0, 0, mPaint);
        for (int i = 0; i < getChildCount(); i++) {
            drawChild(canvas, getChildAt(i), 100);
        } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG, "event Action : " + event.getAction()); 

        if (mVelocityTracker == null)
            mVelocityTracker = VelocityTracker.obtain(); 
       
        mVelocityTracker.addMovement(event);  

        switch (event.getAction()) { 

        case MotionEvent.ACTION_DOWN: 
            if (!mScroller.isFinished()) {  
                mScroller.abortAnimation();
            } 
            mLastPoint.set(event.getX(), event.getY());
            break; 

        case MotionEvent.ACTION_MOVE: 
            int x = (int) (event.getX() - mLastPoint.x); 
            scrollBy(-x, 0); 
            invalidate(); 
            mLastPoint.set(event.getX(), event.getY()); 
            break; 

        case MotionEvent.ACTION_UP: 
            mVelocityTracker.computeCurrentVelocity(1000); 
            int v = (int) mVelocityTracker.getXVelocity(); 

            int gap = getScrollX() - mCurPage * getWidth();
            Log.d(TAG, "mVelocityTracker : " + v); 
            int nextPage = mCurPage; 

            if ((v > SNAP_VELOCITY || gap < -getWidth() / 2) && mCurPage > 0) {
                nextPage--; 
            } else if ((v < -SNAP_VELOCITY || gap > getWidth() / 2) 
                    && mCurPage < getChildCount() - 1) {
                nextPage++; 
            } 

            int move = 0;
            if (mCurPage != nextPage) { 
                move = getChildAt(0).getWidth() * nextPage - getScrollX(); 
            } else {
                move = getWidth() * mCurPage - getScrollX();  
            } 

            mScroller.startScroll(getScrollX(), 0, move, 0, Math.abs(move));
            invalidate(); 
            mCurPage = nextPage;
            mCurTouchState = TOUCH_STATE_NORMAL; 
            mVelocityTracker.recycle(); 
            mVelocityTracker = null; 
            break; 
        } 

        return true; 
    } 

    @Override 
    public void computeScroll() {
        super.computeScroll(); 
        if (mScroller.computeScrollOffset()) {  
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); 
            invalidate(); 
        } 
    } 
    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d(TAG, "onInterceptTouchEvent : " + ev.getAction()); 
        int action = ev.getAction(); 
        int x = (int) ev.getX();
        int y = (int) ev.getY();

        switch (action) { 
        case MotionEvent.ACTION_DOWN: 
            mCurTouchState = mScroller.isFinished() ? TOUCH_STATE_NORMAL 
                    : TOUCH_STATE_SCROLLING; 
            mLastPoint.set(x, y);
            break; 
        case MotionEvent.ACTION_MOVE: 
            int move_x = Math.abs(x - (int) mLastPoint.x);
            if (move_x > mTouchSlop) {  
                mCurTouchState = TOUCH_STATE_SCROLLING;
                mLastPoint.set(x, y); 
            } 
            break; 
        } 

        return mCurTouchState == TOUCH_STATE_SCROLLING; 
    } 
}